#include "cybertron/node/BaseApp.hpp"
#include "cybertron/core/CoreVersion.hpp"
#include "cybertron/core/Log.hpp"
#include "cybertron/core/UtilConsole.hpp"
#include "cybertron/core/logServer.hpp"
#include "cybertron/core/SimOneEnv.hpp"
#include "cybertron/node/StartupArguments.hpp"
#include "cybertron/core/UtilUuidxx.hpp"
#include <iostream>
#include <fstream>
#include <string>

CYBERTRON_BEGIN
    BaseApp* BaseApp::mInstance = nullptr;

    BaseApp::BaseApp() {
        SimOneEnv::GetInstance().Init();
		//BaseApp::mInstance = this;
    }

    BaseApp::~BaseApp() {
    }
    CybertronCoreAPI std::string BaseApp::getVersionOnCore(const std::string& moduleName, const std::string& version)
    {
        try
        {
            nlohmann::json coreVersion = nlohmann::json::parse(cybertron::getCoreVersion());
            nlohmann::json information;
            information["name"] = moduleName;
            information["version"] = version;

            nlohmann::json dependence;
            dependence.push_back(coreVersion);
            information["dependence"] = dependence;

            return information.dump();
        }
        catch (...)
        {
            logError("Unknown errors when parsing json");
        }

        return "";
    }

    CybertronCoreAPI std::string BaseApp::getVersionOnDependence(const std::string& moduleName, const std::string& version, const std::string& dependenceJson)
    {
        try
        {
            nlohmann::json coreVersion = nlohmann::json::parse(cybertron::getCoreVersion());
            nlohmann::json information;
            information["name"] = moduleName;
            information["version"] = version;

            nlohmann::json dependence;
            dependence.push_back(dependenceJson);
            information["dependence"] = dependence;

            return information.dump();
        }
        catch (...)
        {
            logError("Unknown errors when parsing json");
        }

        return "";
    }

    bool BaseApp::init( int argc, char* argv[])
    {
        if (argc > 1 && std::string(argv[1]) == "--version")
        {
            getVerion();
            return false;
        }

        if (!theStartupArguments().parse(argc, argv))
        {
            logError("Failed to parse node startup arguments.");
            for (int i = 1; i < argc; ++i)
            {
                logError("Argument %d: %s", i, argv[i]);
            }

            return false;
        }

        mAppInfo.mAppId = theStartupArguments().getAppId();
        mAppInfo.mAppName = theStartupArguments().getAppName();
        if (parseArguments(argc, argv) != 0)
        {
            return false;
        }

        return true;

    }

    int BaseApp::parseArguments(int argc, char* argv[])
    {
        return 0;
    }

    CybertronCoreAPI bool BaseApp::create()
    {
        /*cybertron::setupLogFile(mAppInfo.mAppName);
        UtilConsole::setTitle(mAppInfo.mAppId);
        UtilConsole::setColor(EConsoleColor_Green, EConsoleColor_Black);*/
        if (beforeRun() != 0)
        {
            return false;
        }

        run();

        return true;
    }

CYBERTRON_END